<template>
  <div>
    <el-form @submit.native.prevent>
      <el-form-item label="条件搜索:">
        <el-input
          @keyup.enter.native="submit"
          v-model="name"
          style="width: 300px"
          placeholder="请输入员工姓名"
        ></el-input>
        <el-button style="margin-left: 20px" type="primary" @click="submit"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-divider>员工信息列表</el-divider>
    <el-table
      :data="tableData"
      :header-cell-style="{
        background: 'radial-gradient(#5f9aff, #50cfff)',
        color: 'white',
      }"
    >
      <el-table-column
        label="员工编号"
        width="100"
        prop="mid"
      ></el-table-column>
      <el-table-column
        label="员工姓名"
        width="140"
        prop="name"
      ></el-table-column>
      <el-table-column label="照片" width="200" prop="pic">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="基本信息" class="table-head">
        <el-table-column label="性别" width="100" prop="sex"></el-table-column>
        <el-table-column
          label="生日"
          width="180"
          prop="birthday"
        ></el-table-column>
        <el-table-column
          label="所属部门"
          width="160"
          prop="department"
        ></el-table-column>
        <el-table-column label="住址" prop="address"></el-table-column>
        <el-table-column label="联系方式" prop="tel"></el-table-column>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="
              msgUpdate(scope.row.name);
              dialogFormVisible = true;
            "
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="memberDel(scope.row.mid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog center title="修改员工信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form" :rules="rules" label-width="140">
        <el-form-item label="姓名:" prop="name">
          <el-input style="width: 300px" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="性别:" prop="sex">
          <el-select
            style="width: 300px"
            v-model="form.sex"
            placeholder="请选择员工性别"
          >
            <el-option
              v-for="item in gender"
              :key="item.gid"
              :label="item.sex"
              :value="item.sex"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="照片:" prop="pic">
          <el-upload
            class="avatar-uploader"
            :action="`${UPLOADURL}/upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.pic" :src="form.pic" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="生日:" prop="birthday">
          <el-date-picker
            style="width: 300px"
            type="date"
            v-model="form.birthday"
            placeholder="选择日期"
            value-format="yyyy-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属部门:" prop="department">
          <el-select v-model="form.department" style="width: 300px">
            <el-option
              v-for="item in departmentList"
              :key="item.did"
              :label="item.department"
              :value="item.department"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="住址:" prop="address">
          <el-input style="width: 300px" v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="联系方式:" prop="tel">
          <el-input style="width: 300px" v-model="form.tel"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="turnIn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      departmentList: [],
      gender: [],
      name: "",
      form: {
        name: "",
        pic: "",
        birthday: "",
        sex: "",
        department: "",
        address: "",
        tel: "",
      },
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        pic: [{ required: true, message: "必填", trigger: "blur" }],
        birthday: [{ required: true, message: "必填", trigger: "blur" }],
        sex: [{ required: true, message: "必填", trigger: "blur" }],
        department: [{ required: true, message: "必填", trigger: "blur" }],
        address: [{ required: true, message: "必填", trigger: "blur" }],
        tel: [
          {
            required: true,
            message: "请填写格式正确的手机号",
            trigger: "blur",
          },
          {
            pattern: /^\w{3,15}$/,
            message: "3~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.list();
    this.memberSex();
    this.memberDepartment();
  },
  methods: {
    turnIn() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.form);
          httpApi.memberApi.memberUpdate(this.form).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "修改员工信息成功" });
              this.dialogFormVisible = false;
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: "修改员工信息失败" });
            }
          });
        }
      });
    },
    list() {
      httpApi.memberApi.memberList().then((res) => {
        this.tableData = res.data.data;
      });
    },
    msgUpdate(name) {
      httpApi.memberApi.memberQuery({ name }).then((res) => {
        this.form = res.data.data[0];
      });
    },
    memberDel(mid) {
      console.log(mid);
      this.$confirm("确认删除该客户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpApi.memberApi.memberDelete({ mid }).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "客户信息删除成功" });
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: "客户信息删除失败" });
            }
          });
        })
        .catch(() => {});
    },
    submit() {
      httpApi.memberApi.memberQuery({ name: this.name }).then((res) => {
        this.tableData = res.data.data;
      });
    },
    memberSex() {
      httpApi.customerApi.querySex().then((res) => {
        this.gender = res.data.data;
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.pic = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    memberDepartment() {
      httpApi.memberApi.queryDepartment().then((res) => {
        this.departmentList = res.data.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .cell {
  text-align: center;
}
::v-deep .el-form-item__label {
  width: 100px;
  text-align-last: justify;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  border: 1px dashed #dcdfe6;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
::v-deep .el-dialog {
  margin-top: 10vh !important;
}
</style>